package cn.wolfcode.rbac.service.impl;

import cn.wolfcode.rbac.domain.Department;
import cn.wolfcode.rbac.mapper.DepartmentMapper;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.IDepartmentService;
import cn.wolfcode.rbac.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @Description TODO
 * @ClassName DepartmentServiceImpl
 * @Author luhui
 * @date 2020.06.13 12:52
 * @version: 1.0
 */
@Service
public class DepartmentServiceImpl implements IDepartmentService {

    @Autowired
    DepartmentMapper departmentMapper;

    @Override
    public void delete(Long id) {
        departmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Department get(Long id) {
        return departmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Department> listAll() {
        return departmentMapper.selectAll();
    }

    @Override
    public void saveOrUpdate(Department department) {
        if (department.getId() == null){
            departmentMapper.insert(department);
        }else {
            departmentMapper.updateByPrimaryKey(department);
        }
    }

    @Override
    public PageResult query(QueryObject queryObject) {
        int count = departmentMapper.selectForCount(queryObject);
        if (count == 0){
            return PageResult.DEFAULT_PAGE;
        }

        List<Department> list = departmentMapper.selectForList(queryObject);
        return new PageResult(queryObject.getCurrentPage(),queryObject.getPageSize(),list,count);
    }
}